package com.gexin.service.product;

import com.gexin.common.model.ApiResponse;
import com.gexin.model.activity.MemberProductIntegral;
import com.gexin.model.enums.IntegralEnum;
import com.gexin.model.product.ProductSku;
import com.gexin.model.product.ProductType;
import com.gexin.model.product.Products;
import com.gexin.model.user.UserCollect;
import com.gexin.model.weixin.WeixinUser;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ProductService {
	public static ProductService dao = new  ProductService();

	/**
	 * 查询热门商品
	 */
	public List<Products> findByHotProduct() {
		List<Products> byHotProduct = Products.dao.findByHotProduct();
		return byHotProduct;
	}

	public List<Products> findByHotProduct(Integer attr_type) {
		List<Products> byHotProduct = Products.dao.findByHotProduct(attr_type);
		return byHotProduct;
	}

	/**
	 * 产品列表
	 */
	public List<ProductType> findProductList() {
		//获取所有类型?
		List<ProductType> all = ProductType.dao.findAll(IntegralEnum.INTEGRAL_ENUM.getType()+"");
		List<MemberProductIntegral> memberProductIntegral = MemberProductIntegral.dao.findAll();
		Map<String, MemberProductIntegral> collect = memberProductIntegral.stream().collect(Collectors.toMap(MemberProductIntegral::getProductId, Function.identity()));

		for (ProductType productType : all) {
			List<Products> normalProduct = Products.dao.findNormalProductByType(productType.getId());
			List<Products> presellProduct = Products.dao.findPresellProductByType(productType.getId());
			productType.put("normalProducts",normalProduct);
			productType.put("presellProduct",presellProduct);
		}
		return all;
	}

	/**
	 * 获取产品详情
	 * @param productId 商品id
	 */
	public Products findProductInfo(Integer productId) {
		Products products = Products.dao.findById(productId);
		List<ProductSku> skus = ProductSku.dao.getStockByProduct(productId);
		products.put("skus",skus);
		return products;
	}

	/**
	 * 用户收藏商品
	 * @param productId 商品id
	 * @param authUser 用户id
	 */
	public ApiResponse addCollection(Integer productId , WeixinUser authUser) {
		if(productId==null){
			return new ApiResponse(false,"商品id不能为null!");
		}
		if(authUser==null){
			return new ApiResponse(false,"用户未登录!");
		}
		UserCollect userCollect = UserCollect.dao.findByProductIdAndUserId(productId , authUser.getId());
		if(userCollect==null){
			Products products = Products.dao.findById(productId);
			if(products==null){
				return new ApiResponse(false,"商品不存在!");
			}
			userCollect = new UserCollect();
			userCollect.setProductId(productId);
			userCollect.setUserId(authUser.getId());
			userCollect.setCreateTime(new Date());
			boolean save = userCollect.save();
			return new ApiResponse(save,save?"收藏成功":"收藏失败");
		}else{
			//如果存在就证明是取消收藏
			boolean delete = userCollect.delete();
			return new ApiResponse(delete,delete?"取消成功":"取消失败");
		}

	}


	/**
	 * 分页获取用户的收藏商品
	 * @param page 当前页
	 * @param size 每页条数
	 * @param user 当前登录用户
	 */
	public Page<UserCollect> findCollectionList(int page , int size , WeixinUser user) {
		return UserCollect.dao.findPageList(page,size,user);
	}
}
